app.controller("HeaderController", ['$scope', '$rootScope', '$location','$window', 'navigationService', 'snsService', '$routeParams', 'globalVariableService','$modal','userService',
    function ($scope, $rootScope, $location, $window, navigationService, snsService, $routeParams, globalVariableService,$modal,userService) {
        if (!getIsFirst()) {
            $modal.open({
                templateUrl: 'views/loading.html',
                controller: ModalAlertCtrl,
                size: 'sm',
                backdrop: 'static',
                resolve: {
                    alertObj: function () {
                        //return {message: 'lkasjfksldfj'};
                    }
                }
            });
            setIsFirst(true);
        }
        $scope.submit = function () {
            console.log("logout!!")
        }
    $scope.back = function () {
        //navigationService.back();
        var backUrl = $routeParams.backUrl;
        //检查是否是在填写我的资料步骤中点击后退，如果是，则全局的路由索引减一
        for(var i = 0;i<globalVariableService.infoNextRoutes.length;++i){
            if(backUrl && backUrl == globalVariableService.infoNextRoutes[i]){
                globalVariableService.currentInd--;
            }
        }
        navigationService.back();
    }
    $rootScope.isPending = true;

    var navBackEventCall=function(evt){
        Log.log("handle NAVIGATION_BACK_EVENT ")
        navigationService.back();
    };
    document.removeEventListener("NAVIGATION_BACK_EVENT",navBackEventCall, false);
    document.addEventListener("NAVIGATION_BACK_EVENT",navBackEventCall, false);
    $scope.iOS7 = iOS7();
    $scope.createClub = function () {
        $location.path("/createClub");
    }
        $scope.showRemovePopover = function(){
            $rootScope.removePopover = true;
        }
        $scope.$on('removePopoverCallback', function (event, data){
            var friendId = {friendId:globalVariableService.userId};
            userService.removeFriend(friendId).then(function(data){
                if(data.status == 0){
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: "删除好友成功"};
                            }
                        }
                    });
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        });
        $scope.removeFriend = function(){
            $rootScope.removePopover = false;
            var modalInstance = $modal.open({
                templateUrl: 'views/tmpl_modal_common_confirm.html',
                controller: ModalCommonMsgCtrl,
                size: 'sm',
                resolve: {
                    obj: function () {
                        return {message:"您确认删除好友?",scope:$scope,callback:"removePopoverCallback"};
                    }
                }
            });
        }

        $scope.cancelDelete = function(){
            $scope.removePopover = false;
        }
}]);

app.controller("SummaryController", ['$scope', '$rootScope', '$location','$window', 'navigationService', 'snsService', '$routeParams', 'globalVariableService',
    function ($scope, $rootScope, $location, $window, navigationService, snsService, $routeParams, globalVariableService) {
        globalVariableService.initRootScope();
        $rootScope.showBackButton = false;
        $rootScope.showHeader = false;
        var diag = new Dialog();
        diag.Title = "设定了高宽和标题的普通窗口";
        diag.URL = "views/tmpl_summary.html";
        diag.show();

    }]);

app.controller("FooterController", ['$scope', '$location', "$rootScope", "$routeParams", function ($scope, $location, $rootScope, $routeParams) {
    $scope.createTopic = function () {
        $location.path("/createTopic/" + $routeParams.snsClubId);
    }
}]);

app.controller("HomeController", ['$location', '$scope', "$rootScope", function ($location, $scope, $rootScope) {
    $rootScope.pageTitle = "";
    $rootScope.pageClass = "page-login";
    $rootScope.showBackButton = false;
    $rootScope.canCreateTopic = false;


}]);

app.controller("AboutController", ['$location', '$scope', "$rootScope", 'globalVariableService','userService', function ($location, $scope, $rootScope, globalVariableService, userService) {
    $rootScope.pageTitle = "关于我们";
    $rootScope.pageClass = "page-about";
    globalVariableService.initRootScope();

    userService.aboutUs().then(function (data) {
        if(data.status == 0){
            $scope.content = data.data.content;
        }
    });

}]);

app.controller("ClubsController", ['$location', '$scope', "$rootScope", "snsService", function ($location, $scope, $rootScope, snsService) {
    $rootScope.pageTitle = "微社区";
    $rootScope.pageClass = "page-clubs";
    $scope.showTopics = true;
    $rootScope.hasFooter = false;
    $rootScope.canCreateTopic = false;
    $scope.isLoading = true;
    $rootScope.isPending = true;

    $scope.isTapping = false;
    $scope.showMore = false;
    $scope.isLoading = false;
    var totalPage = 1;

    var curPage = 1, numPerPage = 10;

    var loadMoreEventHandler = function (evt) {
        if ($scope.isLoading)
            return;

        $scope.isLoading = true;

        /*
         snsService.getTopics({"snsclubid":"","page":curPage,"countPerPage":numPerPage,"sortOption":1}).then(function(data){
         curPage = data.data.page;
         $rootScope.isPending = false;
         $scope.topics = $scope.topics.concat(data.data.topics);
         $scope.isLoading = false;
         });
         */
        var md = $scope.topics[$scope.topics.length - 1].modifiedDecimal;
        snsService.getTopicsAfterModified({
            "snsClubId": "",
            "takeCount": numPerPage,
            "modifiedDecimal": md
        }).then(function (data) {
            //console.log(data.data.topics[0].modifiedDecimal)
            if (data.data.topics.length) {
                $scope.topics = $scope.topics.concat(data.data.topics);
                $scope.isLoading = false;
                $scope.noMorePage = false;
            }
            else {
                $scope.noMorePage = true;
            }
        });
    };

    var getTopics = function () {
        $rootScope.isPending = true;
        //SNS/GetTopicsAfterModified?snsClubId={snsClubId}&modifiedDecimal={modifiedDecimal}&takeCount={takeCount}
        snsService.getTopicsAfterModified({
            "snsClubId": "",
            "takeCount": numPerPage,
            "modifiedDecimal": ""
        }).then(function (data) {
            $scope.topics = data.data.topics;
            $rootScope.isPending = false;
        });
    };
    // remove the event listener
    $rootScope.$$listeners["LOAD_MORE_TITLES_EVENT"] = [];
    $rootScope.$on("LOAD_MORE_TITLES_EVENT", loadMoreEventHandler);
    $rootScope.$on("REFRESH_TITLES_EVENT", getTopics);


    snsService.getUserClubs().then(function (data) {
        if (data.status == 0) {
            $rootScope.isPending = false;
            $rootScope.myClubs = data.data;
        }
    });

    snsService.getRecommendedClubs().then(function (data) {

        if (data.status == 0) {
            $rootScope.isPending = false;
            $scope.recommendedClubs = data.data;
        }
    });
    snsService.getClubs({"page": 1, "countPerPage": 10, "sortClubOption": 1, "countPerPage": 30}).then(function (data) {

        if (data.status == 0) {
            $rootScope.isPending = false;
            $scope.rankClubs = data.data.clubs;
        }
    });
    getTopics();
    /*
     snsService.getTopics({"snsclubid":"","page":1,"countPerPage":numPerPage,"sortOption":1}).then(function(data){
     console.log(data)
     $scope.topics = data.data.topics;
     totalPage = data.data.totalPage;
     });
     */
    $scope.submitSearch = function (searchKey) {

        //console.log($scope.searchOption)
        if (!searchKey)
            return;
        $rootScope.isPending = true;
        snsService.searchClubs({"searchKey": searchKey}).then(function (data) {
            $rootScope.isPending = false;
            if (data.status == 0) {
                $scope.isLoading = false;
                $scope.findClubs = data.data && data.data.length > 0;
                $scope.searchResult = data.data;
                $scope.showSearchResult = true;
                $scope.showTopics = false;
                $scope.showClubs = false;
            }
        });
    };
    $scope.showAllTopics = function () {
        $scope.showSearchResult = false;
        $scope.showTopics = true;
        $scope.showClubs = false;
        $scope.searchOption = "";

    };
    $scope.showAllClubs = function () {
        $scope.showSearchResult = false;
        $scope.showTopics = false;
        $scope.showClubs = true;
        $scope.searchOption = "";
    };
    $scope.gotoClub = function (snsClubId) {

        $rootScope.currentClub = snsClubId;
        $rootScope.canExit = false;
        $location.path("/club/" + snsClubId);
    }
}]);


app.controller("ClubController", ['$location', '$scope', "$rootScope", "$modal", "$routeParams", "snsService", 'globalVariableService', function ($location, $scope, $rootScope, $modal, $routeParams, snsService, globalVariableService) {
    $rootScope.pageTitle = "在线咨询";
    $rootScope.pageClass = "page-club";
    $rootScope.hasFooter = false;
    $rootScope.canCreateTopic = true;
    $rootScope.isPending = true;
    $scope.isJoined = false;
    $scope.inclub = true;
    $scope.isTapping = false;
    $scope.showMore = false;
    $scope.isLoading = false;

    globalVariableService.initRootScope();
    $rootScope.canCreateTopic=true;

    var totalPage = 1;

    var curPage = 1, numPerPage = 10;

    var isInMyClubs = function (clubId, needRemove) {
        if (!$rootScope.myClubs) {
            return false;
        }
        for (var i = 0; i < $rootScope.myClubs.length; i++) {
            if (clubId == $rootScope.myClubs[i].snsClubId) {
                if (needRemove) {
                    $rootScope.myClubs.splice(i, 1);
                    return true;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    var loadMoreEventHandler = function (evt) {
        if ($scope.isLoading)
            return;

        var md = $scope.topics[$scope.topics.length - 1].modifiedDecimal;
        snsService.getTopicsAfterModified({
            "snsClubId": $routeParams.snsClubId,
            "takeCount": numPerPage,
            "modifiedDecimal": md
        }).then(function (data) {
            //console.log(data.data.topics[0].modifiedDecimal)
            if (data.data.topics.length) {
                $scope.topics = $scope.topics.concat(data.data.topics);
                $scope.isLoading = false;
                $scope.noMorePage = false;
            }
            else {
                $scope.noMorePage = true;
            }
        });
    };

    var getTopics = function () {
        $rootScope.isPending = true;
        snsService.getTopicsAfterModified({
            "snsClubId": $routeParams.snsClubId,
            "takeCount": numPerPage,
            "modifiedDecimal": ""
        }).then(function (data) {
            $rootScope.isPending = false;
            $scope.topics = data.data.topics;
            if(data.data.topics <= numPerPage){
                $scope.noMorePage = true;
            }
        });
    };
    // remove the event listener
    $rootScope.$$listeners["LOAD_MORE_TITLES_EVENT"] = [];
    $rootScope.$on("LOAD_MORE_TITLES_EVENT", loadMoreEventHandler);
    $rootScope.$on("REFRESH_TITLES_EVENT", getTopics);

    if (!$rootScope.myClubs) {
        snsService.getUserClubs().then(function (data) {
            if (data.status == 0) {
                $rootScope.myClubs = data.data;
            }
            $scope.isJoined = isInMyClubs($routeParams.snsClubId);
        });
    } else {
        $scope.isJoined = isInMyClubs($routeParams.snsClubId);
    }
    // get current club
    snsService.getClub($routeParams.snsClubId).then(function (data) {

        if (data.status == 0) {
            $scope.club = data.data;
            $rootScope.currentClub = $routeParams.snsClubId;
            getTopics();
        }
    });

}]);

app.controller("RankController", ['$location', '$scope', "$rootScope", "$routeParams", "snsService", function ($location, $scope, $rootScope, $routeParams, snsService) {
    $rootScope.pageTitle = "微社区";
    snsService.getClubs({"page": 1, "countPerPage": 2, "sortClubOption": 1, "countPerPage": 10}).then(function (data) {
        $scope.rankClubs = data.data.clubs;
    });
    $scope.gotoClub = function (snsClubId) {
        $location.path("/club/" + snsClubId);
    };

}]);


app.controller("CreateClubController", ['$location', '$scope', "$rootScope", "$routeParams", "snsService", function ($location, $scope, $rootScope, $routeParams, snsService) {
    $rootScope.pageTitle = "微社区";
    $scope.pageClass = "page-create-club";
    $rootScope.canCreateTopic = true;
}]);

app.controller("CreateTopicController", ['$location', '$scope', "$rootScope", "$routeParams", "snsService", function ($location, $scope, $rootScope, $routeParams, snsService) {
    $rootScope.pageTitle = "微社区";
    $rootScope.pageClass = "page-topic";
    $rootScope.canCreateTopic = false;

    $scope.uploadedImages = [];

    document.removeEventListener("FILE_UPLOADED_EVENT");
    document.addEventListener("FILE_UPLOADED_EVENT", function (evt) {
        $rootScope.isPending = false;
        $scope.isUploadSuccess = true;
        $scope.uploadedImages.push(evt.data);
        $rootScope.$$phase || $rootScope.$apply();
        $scope.$$phase || $scope.$apply();
    }, false);

    document.removeEventListener("FILE_UPLOAD_CANCEL_EVENT");
    document.addEventListener("FILE_UPLOAD_CANCEL_EVENT", function (evt) {
        $rootScope.isPending = false;
        $rootScope.$$phase || $rootScope.$apply();
    }, false);

    $scope.addTopic = function () {
        var content = document.querySelector("textarea[name='topic-content']").value;
        var title = document.querySelector("input[name='topic-title']").value;


        if (!content && !$scope.uploadedImages.length) {
            return;
        }
        var imgs = [];
        angular.forEach($scope.uploadedImages, function (obj) {
            imgs.push(obj.fileUploadId);
        });
        //Log.log("in addTopic function", imgs)
        var param = {
            "snsClubId": $routeParams.snsClubId,
            "title": title,
            "content": content,
            "fileUploadIds": imgs
        };

        snsService.addTopic(param).then(function (data) {
            if (data.status == 0) {
                $scope.uploadedImages = [];
                $location.path("/club/" + $routeParams.snsClubId).replace();
            }
        });
    };

    $scope.removeImage = function (index) {
        $scope.uploadedImages.splice(index, 1);
    };

    //WebViewJavascriptBridge
    $scope.addFile = function () {
        $rootScope.isPending = true;
        $scope.isUploadSuccess = false;
        //$rootScope.$apply();
        //$scope.$apply();
        if (browser.versions.ios) {
            Log.log("try to call ios native to add image");
            if (window.WebViewJavascriptBridge) {
                WebViewJavascriptBridge.callHandler('uploadImage', function (response) {
                    Log.log("call native to upload image");
                });
            }
        } else if (browser.versions.android) {
            Log.log("try to call android native to add image");
            jsb.require('uploadImage', {"msg": "show upload image"}, function (response) {
                Log.log("call native to upload image");
            });

        }
    };

}]);


app.controller("EnrollStatusController", ['$location', '$scope', "$modal", "$rootScope", "$routeParams", "snsService", function ($location, $scope, $modal, $rootScope, $routeParams, snsService) {
    $rootScope.pageTitle = "我的报名";
    $rootScope.pageClass = "page-enroll-status";
    $rootScope.canCreateTopic = false;
    $rootScope.hasFooter = false;
    $rootScope.enrollInfo = {};
    $rootScope.enrollInfo.gender = 0;
    $rootScope.isPending = true;
    snsService.getEnrollmentProfiles().then(function (data) {
        $rootScope.isPending = false;
        $scope.enrollStatus = data.data;
    });
}]);

app.controller("EnrollController", ['$location', '$scope', "$modal", "$rootScope", "$routeParams", "snsService", function ($location, $scope, $modal, $rootScope, $routeParams, snsService) {
    $rootScope.pageTitle = "报名学车";
    $rootScope.pageClass = "page-enroll";
    $rootScope.canCreateTopic = false;
    $rootScope.hasFooter = false;
    $rootScope.enrollInfo = {};
    $rootScope.enrollInfo.gender = 0;
    $rootScope.selectedRegion = $rootScope.selectedRegion ? $rootScope.selectedRegion : {};


    var getProvince = function () {
        $rootScope.isPending = true;
        snsService.getProvince().then(function (data) {
            $rootScope.provinces = data;
            var defaultRegion = findCity(0);
            if (defaultRegion) {
                $rootScope.selectedRegion.provinceName = defaultRegion.province.name;
                $rootScope.selectedRegion.cityName = defaultRegion.city.name;
                $rootScope.selectedRegion.cityId = defaultRegion.city.cityId;
                getSchool(defaultRegion.city.cityId);
            } else {
                $rootScope.isPending = false;

            }

        });
    };
    var getSchool = function (cityId) {
        $rootScope.isPending = true;
        snsService.getDrivingSchool({
            cityId: cityId
        }).then(function (data) {
            if (data.status == 0 && data.data.length) {
                $rootScope.schools = data.data;
                $rootScope.selectedRegion.drivingSchoolId = data.data[0].drivingSchoolId;
                $rootScope.selectedRegion.schoolName = data.data[0].schoolName;
                //console.log(data.data)
            }
            else {

            }

        });
    }
    var findCity = function (cityId) {
        for (var i = 0; i < $rootScope.provinces.length; i++) {
            for (var j = 0; j < $rootScope.provinces[i].cities.length; j++) {
                if (cityId == $rootScope.provinces[i].cities[j].cityId) {

                    return {
                        province: {name: $rootScope.provinces[i].name, provinceId: $rootScope.provinces[i].provinceId},
                        city: {name: $rootScope.provinces[i].cities[j].name, cityId: cityId}
                    };
                }

            }
        }
        return false;

    }


    $scope.selectRegion = function () {
        //console.log("select region")
        schoolWatcher();
        carWatcher();
        classWatcher();
        $rootScope.canExit = false;
        $location.path("/region");
    };
    $scope.selectSchool = function () {
        $rootScope.canExit = false;
        $location.path("/drivingSchool");
    };
    $scope.updateCarType = function (id) {
        $rootScope.selectedRegion.selectedCarType = $scope.carTypeOptions[id];
    };
    $scope.updateClassType = function (id) {
        $rootScope.selectedRegion.selectedClassType = $scope.classTypeOptions[id];
    };
    $scope.updateGenderType = function (gender) {
        $rootScope.enrollInfo.gender = gender;
    };
    $scope.enroll = function () {
        var errMessage;
        if (!getIdCardInfo($rootScope.enrollInfo.idNum || "").isTrue) {
            errMessage = "您填写的身份证号码无效";
        }
        if (!$rootScope.enrollInfo.contactNum || !$rootScope.enrollInfo.name || !$rootScope.enrollInfo.idNum || !$rootScope.selectedRegion.drivingSchoolId) {
            errMessage = "您的个人信息不完整";
        }
        if (errMessage) {
            var modalInstance = $modal.open({
                templateUrl: 'views/tmpl_modal_alert.html',
                controller: ModalAlertCtrl,
                size: 'sm',
                resolve: {
                    alertObj: function () {
                        return {message: errMessage};
                    }
                }
            });
            return;
        }
        ;
        var classTypeId = $rootScope.selectedRegion.selectedClassType ? $rootScope.selectedRegion.selectedClassType.learnDrivingClassTypeId : ''
        var params = {
            "contactNum": $rootScope.enrollInfo.contactNum,
            "name": $rootScope.enrollInfo.name,
            "idNum": $rootScope.enrollInfo.idNum,
            "drivingSchoolId": $rootScope.selectedRegion.drivingSchoolId,
            "gender": $rootScope.enrollInfo.gender,
            "learnDrivingClassTypeId": classTypeId,
            "payMethod": 0,
            "payType": 0
        };
        var modalInstance = $modal.open({
            templateUrl: 'views/tmpl_modal_confirm.html',
            controller: ModalConfirmCtrl,
            size: 'sm',
            resolve: {
                enrollInfo: function () {
                    return params;
                }
            }
        });


    };

    // watch the selected school, call relevant API
    var schoolWatcher = $rootScope.$watch("selectedRegion.drivingSchoolId", function (newVal) {
        if (!newVal) return;
        if ($rootScope.selectedRegion.schoolName) {
            $rootScope.isPending = true;
            snsService.getCarTypes({
                drivingSchoolId: $rootScope.selectedRegion.drivingSchoolId
            }).then(function (data) {
                if (data.data.length) {
                    $scope.carTypeOptions = data.data;
                    $scope.selectedRegion.selectedCarType = data.data[0];
                }
                else {
                    $rootScope.isPending = false;
                }
            });
        }
    });
    // watch the selected car type, call class API
    var carWatcher = $rootScope.$watch("selectedRegion.selectedCarType.learnDrivingCarTypeId", function (newVal) {
        if (!newVal) return;
        if ($rootScope.selectedRegion.selectedCarType) {
            $rootScope.isPending = true;
            snsService.getClassTypes({
                learnDrivingCarTypeId: $rootScope.selectedRegion.selectedCarType.learnDrivingCarTypeId
            }).then(function (data) {
                if (data.data.length) {
                    $scope.classTypeOptions = data.data;
                    $scope.selectedRegion.selectedClassType = data.data[0];
                }
            });
        }
    });
    // watch the selected class type, call price API
    var classWatcher = $rootScope.$watch("selectedRegion.selectedClassType.learnDrivingClassTypeId", function (newVal) {
        if (!newVal) return;
        //console.log("selectedRegion.selectedClassType", $rootScope.selectedRegion.selectedClassType)
        if ($rootScope.selectedRegion.selectedClassType) {
            snsService.getClassPrice({
                learnDrivingClassTypeId: $rootScope.selectedRegion.selectedClassType.learnDrivingClassTypeId
            }).then(function (data) {
                //console.log(data)
                $rootScope.isPending = false;
                $rootScope.selectedRegion.selectedClassPrice = data.data.price;
            });
        }
    });


    if (!$rootScope.provinces) {
        getProvince();
    }
    else {


    }

}]);
app.controller("RegionController", ['$location', '$scope', "$rootScope", "$routeParams", "snsService", function ($location, $scope, $rootScope, $routeParams, snsService) {
    $rootScope.pageTitle = "选择地区";
    $rootScope.pageClass = "page-region";
    $rootScope.cities = [];
    $scope.selectRegion = function () {
        console.log("select region");
    };
    if ($rootScope.regionWatcher) {
        $rootScope.regionWatcher();
    }

}]);


app.controller("DrivingSchoolController", ['$location', '$scope', "$rootScope", "$routeParams", "snsService", function ($location, $scope, $rootScope, $routeParams, snsService) {
    $rootScope.pageTitle = "选择驾校";
    $rootScope.pageClass = "page-school";
    $rootScope.schools = [];
    $scope.hasSchool = false;

    $rootScope.regionWatcher = $rootScope.$watch("selectedRegion", function (newVal) {
        //console.log("$rootScope.provinces[provinceIndex]", $rootScope.cities);
        if (!newVal) return;
        //console.log(cityScroll.wrapper);
        if ($rootScope.selectedRegion.provinceName && $rootScope.selectedRegion.cityName) {
            $rootScope.isPending = true;
            snsService.getDrivingSchool({
                cityId: $rootScope.selectedRegion.cityId
            }).then(function (data) {
                if (data.status == 0 && data.data.length) {
                    $rootScope.schools = data.data;
                }
                else {

                }
                $rootScope.isPending = false;
            });
        }
    });

}]);

var ModalConfirmCtrl = function ($scope, $rootScope, $modalInstance, $modal, snsService, enrollInfo) {
    //var careerData = angular.fromJson(roleInfo);
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    $scope.confirm = function () {
        snsService.apply(enrollInfo).then(function (data) {
            //console.log("=======", data)
            $modalInstance.dismiss('cancel');
            if (data.status == 0) {
                //alert(data.friendlyMessage);
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage, success: true};
                        }
                    }
                });

            } else {
                var errMessage = data.friendlyMessage || "error";

                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: errMessage};
                        }
                    }
                });
            }
        });

    };
};

var ModalAlertCtrl = function ($scope, $location, $rootScope, $modalInstance, snsService, alertObj) {
    //var careerData = angular.fromJson(roleInfo);
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
        if(alertObj.success) {
            //WebViewJavascriptBridge.callHandler('exitUIWebView');
            $location.path("/enrollStatus");
        }
    };
    $scope.alertObj = alertObj;
};

var ModalAddReplyCtrl = function ($scope, $rootScope, $modalInstance, focus, snsService, replyObj) {
    //var careerData = angular.fromJson(roleInfo);
    focus('focusMe');
    $scope.replyObj = replyObj;
    $scope.cancelReply = function (snsTopicId) {
        $modalInstance.dismiss('cancel');
    };
    $scope.replyValue;
    var isSending = false;
    $scope.submitReply = function (snsTopicId) {

        var txt = document.querySelector("textarea[name='replyTopic" + snsTopicId + "']");
        var val = txt.value;
        if (!val)
            return;
        var params = {
            "snsTopicId": snsTopicId,
            "content": val,
            "imgUrls": []
        }
        if (isSending)
            return;
        snsService.reply(params).then(function (data) {
            if (data.status == 0) {
                txt.value = '';
                $modalInstance.dismiss('cancel');

                replyObj.scope.$emit("REFRESH_TOPIC_REPLIES", snsTopicId);
                isSending = false;

            }

        });
        isSending = true;
    };

};


var ModalConfirmDeleteCtrl = function ($scope, $rootScope, $modalInstance, $modal, $location, $routeParams, snsService, removeOption) {
    //var careerData = angular.fromJson(roleInfo);
    $scope.isTopic = removeOption.type == "topic";
    $scope.isReply = removeOption.type == "reply";
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    var isSending = false;
    $scope.confirm = function () {
        if (isSending)
            return;
        if (removeOption.type == "topic") {
            snsService.removeTopic({snsTopicId: removeOption.snsTopicId}).then(function (data) {
                isSending = false;
                $rootScope.$emit("REFRESH_TITLES_EVENT", removeOption.snsTopicId);
                //$location.path("/club/" + $routeParams.snsClubId);
                $modalInstance.dismiss('cancel');
            });

        }
        else if (removeOption.type == "reply") {
            snsService.removeReply({snsReplyId: removeOption.snsReplyId}).then(function (data) {
                isSending = false;
                removeOption.scope.$emit("REFRESH_TOPIC_REPLIES", removeOption.snsTopicId);
                $modalInstance.dismiss('cancel');
            });
        }
        isSending = true;
        //snsService.removeTopic({})
        /*
         snsService.apply(enrollInfo).then(function(data){
         //console.log("=======", data)
         $modalInstance.dismiss('cancel');
         if(data.status==0){
         //alert(data.friendlyMessage);



         }else{
         var errMessage = data.friendlyMessage || "error";


         }
         });
         */
    };
};

app.controller("ImageSliderController", ['$location','$scope', "$rootScope",function($location, $scope, $rootScope){
    $scope.visible = false;
    $rootScope.$on("SHOW_IMAGE_SLIDER_EVENT", function(evt,args){
        Log.log("SHOW_IMAGE_SLIDER_EVENT handler: ");
        $rootScope.isSliderOn = true;
        $scope.content = args;
        $scope.visible = true;
        //document.querySelector(".modal-image-slider").style.display = "";
    });
    $rootScope.$on("HIDE_IMAGE_SLIDER_EVENT", function(evt){
        $scope.content = null;
        $rootScope.isSliderOn = false;
        $scope.visible = false;

        //document.querySelector(".modal-image-slider").style.display = "none";
        return;
    });
}]);